package com.lottery.ruok.lottrey.chat.model.redpacket.message.receiveRedPacketMessage.view;

import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.applozic.mobicomkit.ChatConfig;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.GetPacketResultActivity;
import com.lottery.ruok.lottrey.chat.model.redpacket.message.receiveRedPacketMessage.ReceiveRedPacketMessage;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import utils.StringUtils;

public class ReceiveRedPacketView extends BaseRelativeLayout {
    @BindView(R.id.red_packet)
    ImageView redPacket;
    @BindView(R.id.red_text)
    TextView redText;
    @BindView(R.id.messageLayout)
    RelativeLayout messageLayout;
    private ReceiveRedPacketMessage data;

    public ReceiveRedPacketView(Context context) {
        super(context);
    }

    public ReceiveRedPacketView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ReceiveRedPacketView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.view_receive_red_packet_message);
        redText.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(ReceiveRedPacketMessage data) {
        this.data = data;
        updateView();
    }

    private void updateView() {
        if (data != null) {
            redText.setText(getReceiveTip());
        }
    }

    private CharSequence getReceiveTip() {
        if (data != null) {
            String tip = (isMySelf(data.getReceiveUserNickname()) ? "我" : data.getReceiveUserNickname()) + "抢了" + (isMySelf(data.getSendUserNickname()) ? "我" : data.getSendUserNickname()) + "的红包";
            SpannableString spannableString = new SpannableString(tip);
            spannableString.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    //查看红包详情
                    Intent intent = new Intent(mContext, GetPacketResultActivity.class);
                    intent.putExtra(ChatConfig.PACKET_GET_ID, data.getChildId() + "");
                    intent.putExtra(ChatConfig.PACKET_ID, data.getMsgId() + "");
                    mContext.startActivity(intent);
                }
            }, tip.length() - 2, tip.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            return spannableString;
        }
        return "";
    }

    private boolean isMySelf(String username) {
        if (!StringUtils.isEmpty(username) && username.equals(UserInfo.getNickname())) {
            return true;
        }
        return false;
    }

    public ReceiveRedPacketMessage getData() {
        return data;
    }
}
